<?php
$this->freshSession();
if($this->user['uid']){
	$uid=$this->user['uid'];
	$amount=$_REQUEST['amount'];
	$rechargeId=$this->getRechId();
	$time=date('Y-m-d H:i:s', time());
	//echo json_encode(array('amount'=>$amount));exit;
	if(empty($amount)) {
		$data=array('code'=>0,'result'=>'充值卡密必须填写！');
		echo json_encode($data);exit;
	}
	if(empty($uid)) {
		$data=array('code'=>0,'result'=>'用户已离线，请重新登录！');
		echo json_encode($data);exit;
	}
	$kmsql="select * from km where km=? and status=0";
	$kminfo=$this->getRow($kmsql, $amount);
	if(!$kminfo) {
		$data=array('code'=>0,'result'=>'没有此卡密！');
		echo json_encode($data);exit;
	}
	if($this->update("INSERT INTO {$this->prename}order (order_number, username, recharge_amount, state, time) VALUES('{$rechargeId}', '{$uid}', '{$kminfo['fee']}', '0', '{$time}')")) {
		$para=array();
		$para['mBankId']='123';
		$para['amount']=floatval($kminfo['fee']);
		$para['rechargeId']=$rechargeId;
		$para['actionTime']=$this->time;
		$para['uid']=$this->user['uid'];
		$para['username']=$this->user['username'];
		$para['actionIP']=$this->ip(true);
		$para['info']='卡密充值';
		if($this->insertRow($this->prename .'member_recharge', $para)) {
			if($this->update("update {$this->prename}members set coin=coin+'{$kminfo['fee']}' where uid='{$uid}'")) {
				$this->update("update ssc_member_recharge set state = 1 where rechargeId = '{$para['rechargeId']}'");
				$this->update("insert into usercoin set id = {$para['uid']}, coin = {$para['amount']} on duplicate key update coin = coin + {$para['amount']}");
				$this->update("update km set status=1 where km='{$amount}'");

				$usersql="select * from usercoin where id=?";
				$userinfo=$this->getRow($usersql, $para['uid']);
				$levelsql = "select * from member_level";
				$levelinfo = $this->getRow($levelsql);
				$chatsql = "select * from wwk_chat_user where username = '{$para['username']}'";
				$chatinfo = $this->getRow($chatsql);
				if ($userinfo['coin'] < $chatinfo['password']) {
					$nosql = "update wwk_chat_user set talk = 1 where username = '{$para['username']}'";
					$this->update($nosql);
				}else {
					$nosql = "update wwk_chat_user set talk = 0 where username = '{$para['username']}'";
					$this->update($nosql);
				}
				if ($userinfo['coin'] < $levelinfo['p1']) {
					$this->update("update wwk_chat_user set vip = 1 where username = '{$para['username']}'");
					$data=array('code'=>1,'result'=>"充值{$para['amount']}元成功！");
					echo json_encode($data);exit;
				}elseif ($userinfo['coin'] < $levelinfo['p2']) {
					$this->update("update wwk_chat_user set vip = 2 where username = '{$para['username']}'");
					$data=array('code'=>1,'result'=>"充值{$para['amount']}元成功！");
					echo json_encode($data);exit;
				}elseif ($userinfo['coin'] < $levelinfo['p3']) {
					$this->update("update wwk_chat_user set vip = 3 where username = '{$para['username']}'");
					$data=array('code'=>1,'result'=>"充值{$para['amount']}元成功！");
					echo json_encode($data);exit;
				}elseif ($userinfo['coin'] < $levelinfo['p4']) {
					$this->update("update wwk_chat_user set vip = 4 where username = '{$para['username']}'");
					$data=array('code'=>1,'result'=>"充值{$para['amount']}元成功！");
					echo json_encode($data);exit;
				}elseif ($userinfo['coin'] < $levelinfo['p5']) {
					$this->update("update wwk_chat_user set vip = 5 where username = '{$para['username']}'");
					$data=array('code'=>1,'result'=>"充值{$para['amount']}元成功！");
					echo json_encode($data);exit;
				}else {
					$this->update("update wwk_chat_user set vip = 6 where username = '{$para['username']}'");
					$data=array('code'=>1,'result'=>"充值{$para['amount']}元成功！");
					echo json_encode($data);exit;
				}
			}else {
				$data=array('code'=>0,'result'=>'充值失败！');
				echo json_encode($data);exit;
			}
		}
	}
}
?>